package com.livideo.baseutilslibrary.utils;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 日期工具类
 */
public class DateUtils {

    public static final String ISO_DATE_PATTERN = "yyyy-MM-dd";
    public static final String ISO_DATE_PATTERN2 = "yyyyMMdd";
    public static final String ISO_TIME_PATTERN = "HHmmss";
    public static final String CHINESE_DATE_PATTERN = "yyyy年MM月dd日";
    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_TIME_PATTERN_mm = "yyyy-MM-dd HH:mm";
    public static final String DATE_TIME_PATTERN_hh = "yyyy-MM-dd hh:mm";
    public static final String DATE_PATTERN_POINT = "yyyy.MM.dd";

    /**
     * 获取当前时间
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取默认格式当前日期
     */
    public static String getNowDateDefault() {
        return getFormatDate(DATE_TIME_PATTERN, getNowDate());
    }

    /**
     * 获取默认格式当前日期
     */
    public static String getISONowDate() {
        return getFormatDate(ISO_DATE_PATTERN2, getNowDate());
    }


    /**
     * 获取默认格式当前日期
     */
    public static String getISONowTime() {
        return getFormatDate(ISO_TIME_PATTERN, getNowDate());
    }

    /**
     * Date转String
     * @param format 格式
     * @param date 时间
     */
    public static String getFormatDate(String format, Date date) {
        try {
            return new SimpleDateFormat(format, Locale.getDefault()).format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 时间字符串转换成Date
     * @param format 格式
     * @param dateStr 时间
     */
    public static Date stringToDateParse(String format, String dateStr) {
        try {
            return new SimpleDateFormat(format, Locale.getDefault()).parse(dateStr);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * String时间转换成指定String格式时间
     */
    public static String dateToStringParse(String dateStr, String sdf) {
        if (TextUtils.isEmpty(dateStr)) {
            return "";
        }
        return new SimpleDateFormat(sdf, Locale.getDefault())
                .format(stringToDateParse(dateStr, sdf));
    }

    /**
     * 当前时间小时数 是否 晚于 传入小时数
     * @param hour
     * @return
     */
    public static boolean compareHourAfterEquals(int hour){
        return new GregorianCalendar().get(Calendar.HOUR_OF_DAY) >= hour;
    }

    /**
     * 当前时间小时数 是否 晚于 传入小时数
     * @param hour
     * @return
     */
    public static boolean compareHourAfter(int hour){
        return new GregorianCalendar().get(Calendar.HOUR_OF_DAY) > hour;
    }

    /**
     * 当前时间小时数 是否 早于 传入小时数
     * @param hour
     * @return
     */
    public static boolean compareHourBefore(int hour){
        return new GregorianCalendar().get(Calendar.HOUR_OF_DAY) < hour;
    }

    /**
     * 当前时间小时数 是否 早于 传入小时数
     * @param hour
     * @return
     */
    public static boolean compareHourBeforeEquals(int hour){
        return new GregorianCalendar().get(Calendar.HOUR_OF_DAY) <= hour;
    }

    public static String getDateAboutHour(int hour){
        GregorianCalendar calendar = new GregorianCalendar();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if(compareHourAfter(hour)){
            return "";
        }else{
            return "";
        }
    }

    //判断当前时间是否在时间dateStr之前
    //时间格式 2005-4-21 16:16:34
    public static boolean isDateBefore(String dateStr){
        try{
            Date date1 = new Date();
            SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
            Date date2 = df.parse(dateStr);
            return date1.before(date2);
        }catch(ParseException e){
            return false;
        }
    }
}
